package com.ruoyi.wzgl.gqgl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wzgl.gqgl.domain.Gqgl;
import com.ruoyi.wzgl.gqgl.service.IGqglService;
import com.ruoyi.wzgl.wzxx.domain.CkHjFcVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 工区管理Controller
 *
 * @author huacai
 * @date 2024-09-24
 */
@RestController
@RequestMapping("/wzgl/gqgl")
public class GqglController extends BaseController {
    @Resource
    private IGqglService gqglService;

    /**
     * 查询工区管理列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:gqgl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Gqgl gqgl) {
        startPage();
        List<Gqgl> list = gqglService.selectGqglList(gqgl);
        return getDataTable(list);
    }

    /**
     * 导出工区管理列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:gqgl:export')")
    @Log(title = "工区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Gqgl gqgl) {
        List<Gqgl> list = gqglService.selectGqglList(gqgl);
        ExcelUtil<Gqgl> util = new ExcelUtil<Gqgl>(Gqgl.class);
        util.exportExcel(response, list, "工区管理数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Gqgl> util = new ExcelUtil<Gqgl>(Gqgl.class);
        util.importTemplateExcel(response, "工区管理数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "工区管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wzgl:gqgl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Gqgl> util = new ExcelUtil<Gqgl>(Gqgl.class);
        InputStream inputStream = file.getInputStream();
        List<Gqgl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = gqglService.batchInsertGqgl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取工区管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:gqgl:query')")
    @GetMapping(value = "/{gqglId}")
    public AjaxResult getInfo(@PathVariable("gqglId") Integer gqglId) {
        return success(gqglService.selectGqglByGqglId(gqglId));
    }

    /**
     * 新增工区管理
     */
    @PreAuthorize("@ss.hasPermi('wzgl:gqgl:add')")
    @Log(title = "工区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Gqgl gqgl) {
        gqgl.setUserId(Math.toIntExact(getUserId()));
        gqgl.setDeptId(Math.toIntExact(getDeptId()));
        gqgl.setCreateBy(getUsername());
        return toAjax(gqglService.insertGqgl(gqgl));
    }

    /**
     * 修改工区管理
     */
    @PreAuthorize("@ss.hasPermi('wzgl:gqgl:edit')")
    @Log(title = "工区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Gqgl gqgl) {
        return toAjax(gqglService.updateGqgl(gqgl));
    }

    /**
     * 删除工区管理
     */
    @PreAuthorize("@ss.hasPermi('wzgl:gqgl:remove')")
    @Log(title = "工区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gqglIds}")
    public AjaxResult remove(@PathVariable Integer[] gqglIds) {
        return toAjax(gqglService.deleteGqglByGqglIds(gqglIds));
    }

    /**
     * 查询工区列表(数据权限校验)
     *
     * @return 工区列表(数据权限校验)
     */
    @GetMapping("/selectByQxGq")
    public AjaxResult selectByQxGq() {
        List<Gqgl> list = gqglService.selectByQxGq(new Gqgl());
        return success(list);
    }

}
